using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace Nebular.ExpenseManagement.WebApplication.Controllers
{    
    public class UserController : ControllerBase
    {
        //
        // GET: /User/

        public ActionResult Index(int? page)
        {
            var users = GetMyAccount().Users.AsPagination(page ?? 1);
            return View(users);
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(Guid id)
        {
            var user = GetMyAccount().Users.First(u => u.Id == id);
            return View(user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /User/Create

        IEnumerable<IPrincipal> AllPrincipals(IAccount account)
        {
            return account.Groups.Cast<IPrincipal>()
                .Union(account.Users.Cast<IPrincipal>());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                var account = GetMyAccount();
                var approverId = new Guid(collection["approveBy"]);
                var approver = AllPrincipals(account)
                    .FirstOrDefault(p => p.Id == approverId);
                var creditCards = collection["creditCards"].Split(';');
                account.CreateUser(
                    collection["name"],
                    collection["email"],
                    approver,
                    creditCards);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            var account = GetMyAccount();
            var user = account.Users.First(u => u.Id == id);
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            try
            {
                var account = GetMyAccount();
                var approverId = new Guid(collection["approveBy"]);
                var approver = AllPrincipals(account)
                    .FirstOrDefault(p => p.Id == approverId);
                var creditCards = collection["creditCards"].Split(';');
                account.UpdateUser(
                    id,
                    collection["name"],
                    collection["email"],
                    approver,
                    creditCards);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
